// server.js
const jsonServer = require('json-server')
const server = jsonServer.create()
const router = jsonServer.router('db.json')
const middlewares = jsonServer.defaults()


server.use(middlewares)
server.use(jsonServer.bodyParser); //专门用来处理body数据 不加req.body没效果 req.body 返回json类型

//用户列表接口  已经使用本地db.json 数据 且支持CRUD  jsonplaceholder 网络不稳定
/*
server.get('/users', (req, res) => {
  fetch('https://jsonplaceholder.typicode.com/users')
    .then(response => response.json())
    .then(data => {
      console.log(data)
      res.json(data)
    }).catch(error => {
      console.log("没有查到数据");
      res.json([])
    });

})
*/
//登录接口 密码默认为邮箱
server.post('/login', (req, res) => {
  console.log(req.body.username, req.body.password);
  if(!req.body.username){
    res.status(500)
    res.json({
      success: false,
      code: 500,
      message: "参数异常"

    })
    return;

  }

  if(!req.body.password){
    res.status(500)
    res.json({
      success: false,
      code: 500,
      message: "参数异常"

    })
    return;
  }


  let loginUser;
  fetch('http://localhost:3000/users')
    .then(response => response.json())
    .then(data => {
      for (let user of data) {
        if (user.username === req.body.username) {
          loginUser = user;
          break;
        }

      }
      if (loginUser) {
        if (loginUser.email === req.body.password) {
          res.json({
            success: true,
            code: "200",
            message: "验证通过，可以登录",
            data: {
              token: "dea1a24d5d548b7bc1ec46d974589d0b",  //"lanqiao" md5加密串
              id: loginUser.id,
              name: loginUser.name,
              email: loginUser.email
            }

          });

        } else {
          res.json({
            success: false,
            code: "401",
            message: "密码错误"
          });
        }
      } else {
        res.json({
          success: false,
          code: "404",
          message: "用户不存在"
        });
      }
    }).catch(error => {
      res.status(500)
      res.json({
        success: false,
        code: 500,
        message: "登录异常"

      })
    });
});



//英雄资料接口
server.get('/heros', (req, res) => {
  fetch('https://pvp.qq.com/web201605/js/herolist.json')
    .then(response => response.json())
    .then(data => {
      console.log(data)
      res.json(data)
    }).catch(error => {
      console.log("没有查到数据");
      res.json([])
    });

})

//英雄详细信息接口
server.get('/details', (req, res) => {
  var ename = req.query.ename;
  console.log(ename);
  fetch("https://pvp.qq.com/zlkdatasys/storyhero/index" + ename + ".json")
    .then(response => response.json())
    .then(data => {
      console.log(data)
      res.json(data)
    }).catch(error => {
      console.log("没有查到数据");
      res.json([])
    });

})
//装备资料接口 
server.get('/items', (req, res) => {
  fetch("https://pvp.qq.com/web201605/js/item.json")
    .then(response => response.json())
    .then(data => {
      console.log(data)
      res.json(data)
    }).catch(error => {
      console.log("没有查到数据");
      res.json([])
    });

})

//召唤师资料接口
server.get('/summoners', (req, res) => {
  fetch("https://pvp.qq.com/web201605/js/summoner.json")
    .then(response => response.json())
    .then(data => {
      console.log(data)
      res.json(data)
    }).catch(error => {
      console.log("没有查到数据");
      res.json([])
    });

})

//铭文资料接口
server.get('/mings', (req, res) => {
  fetch("https://pvp.qq.com/web201605/js/ming.json")
    .then(response => response.json())
    .then(data => {
      console.log(data)
      res.json(data)
    }).catch(error => {
      console.log("没有查到数据");
      res.json([])
    });

})


server.use(router)
server.listen(3000, () => {
  console.log('JSON Server is running   port  3000 . /heros ,/users  ,/details?ename=106')
})